package adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.administrator.ci_edit.R;

import java.util.List;

import db.CipaiDatabaseHelper;
import entity.CipaiInfo;

import static com.example.administrator.ci_edit.R.color.blue;

/**
 * Created by hello world on 2017/7/13.
 */

public class CipaiInfoAdapter extends ArrayAdapter<CipaiInfo> {
    private int resourceId;

    public CipaiInfoAdapter(Context context, int textViewResourceId, List<CipaiInfo> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        CipaiInfo cipaiInfo = getItem(position);


        //View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
        //用新的方式，提升ListView的运行效率
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        } else {
            view = convertView;
        }
        //ImageView cipaiImage=(ImageView)view.findViewById(R.id.cipai_image);
        TextView cipaiNameTV = (TextView) view.findViewById(R.id.cipai_name);
        TextView cipaiEnNameTV = (TextView)view.findViewById(R.id.cipai_enname) ;
        TextView wordcountTV = (TextView)view.findViewById(R.id.wordcount);
        //cipaiImage.setImageResource(cipaiInfo.getImageId());
        cipaiNameTV.setText(cipaiInfo.getName());
        cipaiEnNameTV.setText(cipaiInfo.getEnname());
        wordcountTV.setText(cipaiInfo.getWordcount());
        //// TODO: 2017/7/24 color
        CipaiDatabaseHelper cipaiDatabaseHelper = new CipaiDatabaseHelper();
        Cursor cursor = cipaiDatabaseHelper.getRGBByCipaiId(cipaiInfo.getId());
        while (cursor.moveToNext()){
            int red = cursor.getInt(cursor.getColumnIndex("red"));
            int green = cursor.getInt(cursor.getColumnIndex("green"));
            int blue = cursor.getInt(cursor.getColumnIndex("blue"));
            cipaiNameTV.setTextColor(Color.rgb(red,green,blue));
            cipaiEnNameTV.setTextColor(Color.rgb(red,green,blue));
            wordcountTV.setTextColor(Color.rgb(red,green,blue));
        }

//        Cursor cursor1 = cipaiDatabaseHelper.getAllCipaiName();
//        while (cursor1.moveToNext()){
//            String enname = cursor1.getString(cursor1.getColumnIndex("enname"));
//            String wordcount = cursor1.getString(cursor1.getColumnIndex("wordcount"));
//            cipaiEnNameTV.setText(enname);
//            wordcountTV.setText(wordcount);
//        }

        return view;
        //return super.getView(position, convertView, parent);
    }
}
